package com.rock.code.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author zhangli
 * @version V1.0
 * @create 2019-02-26 18:43
 **/
public class StartupListener implements ServletContextListener {
    private static final Log log = LogFactory.getLog(StartupListener.class);

    public StartupListener() {
    }

    public void contextInitialized(ServletContextEvent event) {
        log.debug("Initializing context...");
        ServletContext context = event.getServletContext();
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        setupContext(context);
        SpringContext.initialize(context);

        try {
            Server server = (Server)SpringContext.getBean("server");
            System.out.println("server start begin.");
            server.start();
            System.out.println("server start end.");
        } catch (Exception var5) {
            System.out.println("No Server Config!");
        }

    }

    public static void setupContext(ServletContext context) {
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
        log.debug("Drop-down initialization complete [OK]");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}